package com.hotel.tv.application.admin.services.auth.impl;

import com.autumn.mybatis.wrapper.EntityQueryWrapper;
import com.autumn.zero.authorization.application.dto.roles.RoleDto;
import com.autumn.zero.authorization.application.dto.roles.RoleInput;
import com.autumn.zero.authorization.application.dto.roles.RoleOutput;
import com.autumn.zero.authorization.application.services.impl.AbstractRoleAppService;
import com.hotel.tv.application.admin.services.auth.RoleAppService;
import com.hotel.tv.entities.auth.HotelRole;
import com.hotel.tv.entities.auth.HotelUser;
import com.hotel.tv.repositories.auth.HotelRoleRepository;
import com.hotel.tv.runtime.session.HotelAdminSession;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

/**
 * <p>
 * </p>
 *
 * @description TODO
 * @author: 老码农
 * @create: 2020-04-10 12:22
 **/
@Service
@Primary
public class RoleAppServiceImpl extends AbstractRoleAppService<HotelRole,
        HotelUser,
        HotelRoleRepository,
        RoleInput, RoleInput, RoleDto, RoleOutput>
        implements RoleAppService {

    public RoleAppServiceImpl() {
        super(HotelRole.class, RoleDto.class, RoleOutput.class);
    }

    @Override
    protected void systemByCriteria(EntityQueryWrapper<HotelRole> wrapper) {
        super.systemByCriteria(wrapper);
        HotelAdminSession session = (HotelAdminSession) this.getSession();
        if (session.isAdministratorUser()) {
            wrapper.lambda().where().eq(HotelRole::getUserType, session.getUserType());
        } else {
            wrapper.lambda().where().eq(HotelRole::getUserType, session.getUserType())
                    .eq(HotelRole::getOrganizeId, session.getOrganizeId());
        }
    }

    @Override
    protected void setRoleDefault(HotelRole entity) {
        super.setRoleDefault(entity);
        HotelAdminSession session = (HotelAdminSession) this.getSession();
        entity.setUserType(session.getUserType());
        entity.setOrganizeId(session.getOrganizeId());
    }
}
